from django import forms
from ai_inspect.models import StepRule, PeriodRule, Channel, Station, StationType, StationBattery
from ai_inspect.models import AiBox, RobotType, Algorithm


class AlgorithmForm(forms.ModelForm):
    SWAP_MAKERS = [
        ('玖行', '玖行'),
        ('融青', '融青'),
        ('智锂', '智锂')
    ]
    swap_maker = forms.MultipleChoiceField(
        label="适配换电站厂家",
        choices=SWAP_MAKERS,
        widget=forms.CheckboxSelectMultiple,
        required=True
    )
    inspect_type = forms.ChoiceField(
        label="巡检算法类型",
        choices=[('基于换电步骤', '基于换电步骤'), ('周期性', '周期性')],
        widget=forms.RadioSelect(attrs={"class": "form-check-inline"}),
    )

    class Meta:
        model = Algorithm
        fields = ['algorithm_name', 'algorithm_topic', 'inspect_result_topic', 'intermediate_result_topic',
                  'swap_maker', 'alarm_name', 'inspect_type']
        widgets = {
            'algorithm_name': forms.TextInput(attrs={'class': 'form-control'}),
            'algorithm_topic': forms.TextInput(attrs={'class': 'form-control'}),
            'inspect_result_topic': forms.TextInput(attrs={'class': 'form-control'}),
            'intermediate_result_topic': forms.TextInput(attrs={'class': 'form-control'}),
            'alarm_name': forms.TextInput(attrs={'class': 'form-control'}),
        }


class StepRuleForm(forms.ModelForm):
    class Meta:
        model = StepRule
        fields = ['station', 'algorithm', 'channel', 'step', 'activate']


class PeriodRuleForm(forms.ModelForm):
    class Meta:
        model = PeriodRule
        fields = ['station', 'algorithm', 'channel', 'period', 'activate']


class ChannelForm(forms.ModelForm):
    class Meta:
        model = Channel
        fields = ['channel_number', 'channel_name', 'aibox_sn', 'channel_ip',
                  'channel_url', 'channel_algorithm']


class AiboxCreateForm(forms.ModelForm):
    ai_inspect = forms.ChoiceField(
        label="是否支持智能巡检",
        choices=[(True, '是'), (False, '否')],
        widget=forms.RadioSelect(attrs={"class": "form-check-inline"}),
        initial=True
    )
    aibox_type = forms.ChoiceField(
        label="视频推理机型号",
        choices=[('MiniBox-H8', '一代'), ('TD-N7116', '二代')],
        widget=forms.RadioSelect(attrs={"class": "form-check-inline"}),
        initial="TD-N7116"
    )

    class Meta:
        model = AiBox
        fields = ['station_id', 'aibox_sn', 'aibox_url', 'aibox_type', 'ai_inspect']
        widgets = {
            # 将choices渲染为单选按钮组
            'aibox_sn': forms.TextInput(attrs={'class': 'form-control'}),
            'aibox_url': forms.TextInput(attrs={'class': 'form-control', 'value': 'http://119.45.171.209:5'}),
        }
        labels = {
            "aibox_sn": "产品序列号",
            "aibox_url": "远程访问URL",
        }


class AiboxUpdateForm(forms.ModelForm):
    ai_inspect = forms.ChoiceField(
        label="是否支持智能巡检",
        choices=[(True, '是'), (False, '否')],
        widget=forms.RadioSelect(attrs={"class": "form-check-inline"}),
        initial=True
    )
    aibox_type = forms.ChoiceField(
        label="视频推理机型号",
        choices=[('MiniBox-H8', '一代'), ('TD-N7116', '二代')],
        widget=forms.RadioSelect(attrs={"class": "form-check-inline"}),
        initial="TD-N7116"
    )

    class Meta:
        model = AiBox
        fields = ['station_id', 'aibox_sn', 'aibox_url', 'ai_inspect', 'aibox_type', 'monitor_channel']
        widgets = {
            # 将choices渲染为单选按钮组
            'aibox_sn': forms.TextInput(attrs={'class': 'form-control'}),
            'aibox_url': forms.TextInput(attrs={'class': 'form-control', 'value':'http://119.45.171.209:5'}),
        }
        labels = {
            "aibox_sn": "产品序列号",
            "aibox_url": "远程访问URL",
        }


class StationForm(forms.ModelForm):
    station_type = forms.ModelChoiceField(
        label="换电站型号",
        queryset=StationType.objects.all(),
        empty_label="请选择",
        widget=forms.Select(attrs={"class": "form-control"}),
    )
    robot_type = forms.ModelChoiceField(
        label="换电机器人型号",
        queryset=RobotType.objects.all(),
        widget=forms.Select(attrs={"class": "form-control"})
    )

    class Meta:
        model = Station
        fields = ['station_id', 'station_name', 'business_unit', 'group_id', 'software_version',
                  'station_type', 'robot_type', 'station_maker']
        widgets = {
            # 将choices渲染为单选按钮组
            "station_id": forms.TextInput(attrs={"class": "form-control"}),
            "station_name": forms.TextInput(attrs={"class": "form-control"}),
            'business_unit': forms.Select(attrs={'class': 'form-control'}),
            'group_id': forms.Select(attrs={'class': 'form-control'}),
            'station_maker': forms.Select(attrs={'class': 'form-control'}),
            'software_version': forms.Select(attrs={'class': 'form-control'}),
            'station_type': forms.Select(attrs={'class': 'form-control'}),
            'robot_type': forms.Select(attrs={'class': 'form-control'}),
        }
        labels = {
            "station_id": "换电站编号",
            "station_name": "换电站名称",
            "business_unit": "事业部",
            "group_id": "钉钉群",
            "software_version": "站控类型",
            "station_type": "换电站型号",
            "station_maker": "生产厂商",
            "robot_type": "机器人型号",
        }


class StationBatteryForm(forms.ModelForm):
    class Meta:
        model = StationBattery
        fields = ['station_id', 'battery_count_validate', 'validate_time']